const express = require('express');
const router = express.Router();
const MySqlHelper = require('../common/MySqlHelper');
const client = require('../common/ServiceClient');
const MsgJsonHelper = require('../common/MsgJsonHelper');
const QueryModel = require('../common/QueryModel');
const MemoryCondition = require('../common/MemoryCondition');
const {
	DataAccess,
	MType,
	MLogic,
	MOperator,
	SortParam,
	Direction,
	OperationEnum,
	Public,
} = require('msg-dataaccess-base');
const Routebase = require('./route.base');
const ZK_LIULIANG = require('../model/ZK_LIULIANG');

const ds = new DataAccess();

router.all('*', (req, res, next) => {
	if (Routebase.IsLogin(req, res)) {
		if (Routebase.IsPermit(req, res, '00018')) {
			next();
		}
	}
});

/**
 * 访问图表(原始数据，未分组)
 */
router.get('/charts', (req, res) => {
	let start = req.query['START'] === undefined ? '' : req.query['START'].toString();
	let end = req.query['END'] === undefined ? '' : req.query['END'].toString();
	if (!start || !end) {
		return res.json(MsgJsonHelper.DebugJson('参数异常'));
	}
	let sqlsrt =
		"select * from ZK_LIULIANG where ZK_TIME Between '" +
		start +
		"' and '" +
		end +
		"' and EB_ISDELETE = '0' order by ZK_TIME asc";
	ds.GetTable(sqlsrt)
		.then(dt => {
			if (dt.length > 0) {
				res.json(MsgJsonHelper.DefaultJson(dt, true, dt.toString()));
			} else {
				res.json(MsgJsonHelper.DebugJson('暂无更多信息'));
			}
		})
		.catch(() => {
			res.json(MsgJsonHelper.DebugJson('流量图表接口请求异常'));
		});
});

module.exports = router;
